import numpy as np
import pandas as pd

from init import process_house_sales

# 获取预先处理的数据
house_sales = process_house_sales()

def get_column_list():
    return house_sales.select_dtypes(include=np.number).columns

# 数值型列的描述性统计指标
def describe_house_desc():
    return house_sales[get_column_list()].describe()

# 不同特征与房价的相关性
def house_sales_price_corr():
    corr = house_sales.corr()
    return corr['price']

# 按邮政编码统计指标
def house_sales_zipcode_agg():
    house_sales_group = house_sales.groupby(['zipcode'])
    return house_sales_group.agg({'price': 'mean', 'sqft_living': 'mean', 'bedrooms': 'mean'})

# 按是否翻新统计指标
def house_sales_is_renovated_agg():
    return house_sales.pivot_table(values=['price', 'sqft_living', 'bedrooms'], columns=['is_renovated'], aggfunc='mean', observed=True)

# 按房龄统计指标
def house_sales_age_built_agg():
    house_sales['age_built_group'] = pd.cut(house_sales['age_built'], 5)
    return house_sales.groupby('age_built_group', observed=True)[['price', 'sqft_living', 'bedrooms']].mean()

# 时间序列分析每年平均销售价格
def house_sales_year_price_mean():
    return pd.pivot_table(data=house_sales, values='price', index=house_sales['date'].dt.year, aggfunc='mean', observed=True)

# print(house_sales_price_corr())